<template>
    <!-- <div> -->
    <a-card title="基带控制">
        <template #extra>
            <a-button type="primary" slot="tabBarExtraContent" style="margin-right: 10px;"
                @click="handleSend">配置</a-button>
            <a-button type="primary" slot="tabBarExtraContent" @click="resetForm">重置</a-button>
        </template>
        <a-form-model ref="ruleForm" :rules="rules" :model="formData" :label-col="labelCol" :wrapper-col="wrapperCol">
            <!-- <a-form-model-item label="状态">
                    <a-select v-model="formData.status" placeholder="">
                        <a-select-option value="0">
                            复位状态
                        </a-select-option>
                        <a-select-option value="1">
                            工作状态
                        </a-select-option>
                    </a-select>
                </a-form-model-item> -->
            <a-row>
                <a-col :span="12">
                    <a-form-model-item label="上行工作模式" prop='UpwardWorkingMode'>
                        <a-select v-model="formData.UpwardWorkingMode" placeholder="">
                            <a-select-option value="0">
                                DBPSK
                            </a-select-option>
                            <a-select-option value="1">
                                DQPSK
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="下行工作模式" prop='downwardWorkingMode'>
                        <a-select v-model="formData.downwardWorkingMode" placeholder="">
                            <a-select-option value="0">
                                DBPSK
                            </a-select-option>
                            <a-select-option value="1">
                                DQPSK
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="上行频率模式" prop='UplinkFrequencyMode'>
                        <a-select v-model="formData.UplinkFrequencyMode" placeholder="">
                            <a-select-option value="0">
                                定频模式
                            </a-select-option>
                            <a-select-option value="1">
                                跳频模式
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="下行频率模式" prop='DownlinkFrequencyMode'>
                        <a-select v-model="formData.DownlinkFrequencyMode" placeholder="">
                            <a-select-option value="0">
                                定频模式
                            </a-select-option>
                            <a-select-option value="1">
                                跳频模式
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="上行频率值" prop="UplinkFrequencyValue">
                        <a-select v-model="formData.UplinkFrequencyValue" placeholder="">
                            <a-select-option value="39387000">
                                960MHz
                            </a-select-option>
                            <a-select-option value="47868C00">
                                1200MHz
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="下行频率值" prop="DownlinkFrequencyValue">
                        <a-select v-model="formData.DownlinkFrequencyValue" placeholder="">
                            <a-select-option value="39387000">
                                960MHz
                            </a-select-option>
                            <a-select-option value="47868C00">
                                1200MHz
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="上行同步时隙开关" prop="UplinkSynchronization">
                        <a-radio-group v-model="formData.UplinkSynchronization" style="margin-left: 10px;">
                            <a-radio value="1000">
                                1
                            </a-radio>
                            <a-radio value="0100">
                                2
                            </a-radio>
                            <a-radio value="0010">
                                3
                            </a-radio>
                            <a-radio value="0001">
                                4
                            </a-radio>
                        </a-radio-group>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="上行业务时隙开关" prop="UpstreamBusiness">
                        <a-checkbox-group v-model="formData.UpstreamBusiness" style="margin-left: 10px;">
                            <a-checkbox value="1">
                                1
                            </a-checkbox>
                            <a-checkbox value="2">
                                2
                            </a-checkbox>
                            <a-checkbox value="3">
                                3
                            </a-checkbox>
                            <a-checkbox value="4">
                                4
                            </a-checkbox>
                            <a-checkbox value="5">
                                5
                            </a-checkbox>
                        </a-checkbox-group>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="中频切换" prop="IntermediateFrequencySwitching">
                        <a-select @change='changeIntermediateFrequency'
                            v-model="formData.IntermediateFrequencySwitching" placeholder="">
                            <a-select-option value="0">
                                960MHz
                            </a-select-option>
                            <a-select-option value="1">
                                1200MHz
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="跳频模式同步信道频率" prop="SynchronousChannelFrequency">
                        <a-select disabled v-model="formData.SynchronousChannelFrequency" placeholder="">
                            <a-select-option value="39387000">
                                960MHz
                            </a-select-option>
                            <a-select-option value="47868C00">
                                1200MHz
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="站地址" prop="StationAddress">
                        <a-input v-model="formData.StationAddress" placeholder="请输入十六进制数"> </a-input>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="单向星地传输延迟(us)" prop="TransmissionDelay">
                        <a-input v-model="formData.TransmissionDelay"> </a-input>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="上行频率切换时刻(us)" prop="UplinkFrequencySwitchingTime">
                        <a-input v-model="formData.UplinkFrequencySwitchingTime"> </a-input>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="下行频率切换时刻(us)" prop="DownlinkFrequencySwitchingTime">
                        <a-input v-model="formData.DownlinkFrequencySwitchingTime"> </a-input>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="上行跳频频偏补偿(Hz)" prop="FrequencyOffsetCompensation">
                        <a-input v-model="formData.FrequencyOffsetCompensation"> </a-input>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="上下行脉冲调整时间(us)" prop="PulseAdjustmentTime">
                        <a-input v-model="formData.PulseAdjustmentTime"> </a-input>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="上行跳频图案" prop="UpwardFrequencyHoppingPattern">
                        <a-textarea v-model="formData.UpwardFrequencyHoppingPattern" placeholder="输入长度32768" :rows="4"
                            :maxLength="32768" />
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="下行调频图案" prop="DownwardFrequencyHoppingPattern">
                        <a-textarea v-model="formData.DownwardFrequencyHoppingPattern" placeholder="输入长度32768" :rows="4"
                            :maxLength="32768" />
                    </a-form-model-item>
                </a-col>
            </a-row>

        </a-form-model>
    </a-card>
    <!-- </div> -->
</template>

<script>
import { httpAction } from '../../../api/manage';


export default {
    // name: 'baseConfig',
    data() {
        const validateHexStationAddress = (rule, value, callback) => {
            let hexPattern = /^[0-9a-fA-F]+$/
            if (!hexPattern.test(value)) {
                callback(new Error('请输入十六进制数'));
            } else {
                callback();
            }
        }
        const validateHexTransmissionDelay = (rule, value, callback) => {
            let hexPattern = /^[0-9a-fA-F]+$/
            if (!hexPattern.test(value)) {
                callback(new Error('请输入十六进制数'));
            } else {
                callback();
            }
        }
        const validateHexUplinkFrequencySwitchingTime = (rule, value, callback) => {
            let hexPattern = /^[0-9a-fA-F]+$/
            if (!hexPattern.test(value)) {
                callback(new Error('请输入十六进制数'));
            } else {
                callback();
            }
        }
        const validateHexDownlinkFrequencySwitchingTime = (rule, value, callback) => {
            let hexPattern = /^[0-9a-fA-F]+$/
            if (!hexPattern.test(value)) {
                callback(new Error('请输入十六进制数'));
            } else {
                callback();
            }
        }
        const validateHexFrequencyOffsetCompensation = (rule, value, callback) => {
            let hexPattern = /^[0-9a-fA-F]+$/
            if (!hexPattern.test(value)) {
                callback(new Error('请输入十六进制数'));
            } else {
                callback();
            }
        }

        const validateHexPulseAdjustmentTime = (rule, value, callback) => {
            let hexPattern = /^[0-9a-fA-F]+$/
            if (!hexPattern.test(value)) {
                callback(new Error('请输入十六进制数'));
            } else {
                callback();
            }
        }

        const validateHexUpwardFrequencyHoppingPattern = (rule, value, callback) => {
            let hexPattern = /^[0-9a-fA-F]+$/
            if (!hexPattern.test(value)) {
                callback(new Error('请输入十六进制数'));
            } else {
                callback();
            }
        }

        const validateHexDownwardFrequencyHoppingPattern = (rule, value, callback) => {
            let hexPattern = /^[0-9a-fA-F]+$/
            if (!hexPattern.test(value)) {
                callback(new Error('请输入十六进制数'));
            } else {
                callback();
            }
        }
        return {
            formData: {
                // downwardWorkingMode: '0'
            },
            labelCol: { span: 9 },
            wrapperCol: { span: 15 },
            url: {
                sendDevicePackage: "/device/nmcDeviceGridValue/sendDevicePackage"
            },
            rules: {
                UpwardWorkingMode: [{ required: true, message: '请选择', trigger: 'change,blur' }],
                downwardWorkingMode: [{ required: true, message: '请选择', trigger: 'change,blur' },],
                UplinkFrequencyMode: [{ required: true, message: '请选择', trigger: 'change,blur' },],
                DownlinkFrequencyMode: [{ required: true, message: '请选择', trigger: 'change,blur' },],
                UplinkFrequencyValue: [{ required: true, message: '请选择', trigger: 'change,blur' },],
                DownlinkFrequencyValue: [{ required: true, message: '请选择', trigger: 'change,blur' },],
                UplinkSynchronization: [{ required: true, message: '请选择', trigger: 'change,blur' },],
                UpstreamBusiness: [{ required: true, message: '请选择', trigger: 'change,blur' },],
                IntermediateFrequencySwitching: [{ required: true, message: '请选择', trigger: 'change,blur' },],
                StationAddress: [{ required: true, message: '请输入', trigger: 'change,blur' }, { validator: validateHexStationAddress }],
                TransmissionDelay: [{ required: true, message: '请输入', trigger: 'change,blur' }, { validator: validateHexTransmissionDelay }],
                UplinkFrequencySwitchingTime: [{ required: true, message: '请输入', trigger: 'change,blur' }, { validator: validateHexUplinkFrequencySwitchingTime }],
                DownlinkFrequencySwitchingTime: [{ required: true, message: '请输入', trigger: 'change,blur' }, { validator: validateHexDownlinkFrequencySwitchingTime }],
                FrequencyOffsetCompensation: [{ required: true, message: '请输入', trigger: 'change,blur' }, { validator: validateHexFrequencyOffsetCompensation }],
                PulseAdjustmentTime: [{ required: true, message: '请输入', trigger: 'change,blur' }, { validator: validateHexPulseAdjustmentTime }],
                UpwardFrequencyHoppingPattern: [{ required: true, message: '请输入', trigger: 'change,blur' }, { validator: validateHexUpwardFrequencyHoppingPattern }],
                DownwardFrequencyHoppingPattern: [{ required: true, message: '请输入', trigger: 'change,blur' }, { validator: validateHexDownwardFrequencyHoppingPattern }],
            }


        }
    },
    methods: {
        changeIntermediateFrequency(val) {
            if (val == 0) {
                this.formData.SynchronousChannelFrequency = '39387000'
            } else if (val == 1) {
                this.formData.SynchronousChannelFrequency = '47868C00'
            }
        },
        resetForm() {
            this.formData = {}
            this.$refs.ruleForm.resetFields();
        },
        replaceNthBit(str, n, newValue) {
            // 将字符串转换为数组
            var charArray = str.split('');

            // 确保n是有效位置
            if (n >= 0 && n < charArray.length) {
                // 将第n位的数字替换为新值
                charArray[n] = newValue;
            }

            // 将数组重新组合为字符串
            return charArray.join('');
        },
        sendAction(data) {
            httpAction(this.url.sendDevicePackage, data, 'post').then(res => {
                if (res.success) {
                    this.$message.success(res.message)
                } else {
                    this.$message.error(res.message)
                }

            })
        },
        configUpstream() {
            let UpwardWorkingMode = '0000' + this.formData.UplinkSynchronization + this.formData.UplinkSynchronization + this.formData.UplinkSynchronization
            // console.log(parseInt(UpwardWorkingMode, 2).toString(16).padStart(4, 0))
            // let UpstreamBusiness = '000' + this.UpstreamBusiness
            // console.log(this.formData.UpstreamBusiness)
            let a = '00000'
            this.formData.UpstreamBusiness.forEach(i => {
                a = this.replaceNthBit(a, (i * 1) - 1, '1')

            });
            let UpstreamBusiness = '000' + a
            // return
            let data = {
                "channelCode": "基带",
                "gridValues": [
                    {
                        "gridCode": "144-上行工作模式",
                        "gridValue": this.formData.UpwardWorkingMode
                    },
                    {
                        "gridCode": "142-上行同步时隙开关",
                        "gridValue": parseInt(UpwardWorkingMode, 2).toString(16).padStart(4, 0)
                    },
                    {
                        "gridCode": "142-上行业务时隙开关",
                        "gridValue": parseInt(UpstreamBusiness, 2).toString(16).padStart(2, 0)
                    },
                    {
                        "gridCode": "142-站地址",
                        "gridValue": this.formData.StationAddress
                    },
                ],
                "packageCode": "142-jd-上行-控制接口数据下发"
            }
            this.sendAction(data)
            // let flag = true
            // data.gridValues.forEach(e => {
            //     if (e.gridValue == undefined || e.gridValue == '' || e.gridValue == null) {
            //         var index = e.gridCode.lastIndexOf("\-");
            //         let str = e.gridCode.substring(index + 1, e.gridCode.length);
            //         this.$message.error(`请输入${str}`)
            //         flag = false
            //     }
            // })
            // if (flag) this.sendAction(data)
        },
        configFrequencyHopping() {
            let mode = this.formData.DownlinkFrequencyMode + this.formData.UplinkFrequencyMode
            // return
            let data = {
                "channelCode": "基带",
                "gridValues": [
                    {
                        "gridCode": "146-定频跳频模式选择",
                        "gridValue": mode
                    },
                    {
                        "gridCode": "146-下行定频频率值",
                        "gridValue": this.formData.DownlinkFrequencyValue
                    },
                    {
                        "gridCode": "146-上行定频频率值",
                        "gridValue": this.formData.UplinkFrequencyValue
                    },
                    {
                        "gridCode": "146-中频切换",
                        "gridValue": this.formData.IntermediateFrequencySwitching
                    },

                    {
                        "gridCode": "146-跳频模式下同步信道频率",
                        "gridValue": this.formData.SynchronousChannelFrequency
                    },
                    {
                        "gridCode": "146-单向星地传输延迟",
                        "gridValue": this.formData.TransmissionDelay
                    },
                    {
                        "gridCode": "146-下行频率切换时刻",
                        "gridValue": this.formData.DownlinkFrequencySwitchingTime
                    },
                    {
                        "gridCode": "146-上行频率切换时刻",
                        "gridValue": this.formData.UplinkFrequencySwitchingTime
                    },
                    {
                        "gridCode": "146-上下行脉冲调整时间",
                        "gridValue": this.formData.PulseAdjustmentTime
                    },
                    {
                        "gridCode": "146-上行跳频频偏补偿",
                        "gridValue": this.formData.FrequencyOffsetCompensation
                    },

                ],
                "packageCode": "146-jd-跳频-数据下发"
            }
            // let flag = true
            // data.gridValues.forEach(e => {
            //     if (e.gridValue == undefined || e.gridValue == '' || e.gridValue == null) {
            //         var index = e.gridCode.lastIndexOf("\-");
            //         let str = e.gridCode.substring(index + 1, e.gridCode.length);
            //         this.$message.error(`请输入${str}`)
            //         flag = false
            //     }
            // })
            // if (flag) this.sendAction(data)
            this.sendAction(data)
        },
        configDown() {

            let data = {
                "channelCode": "基带",
                "gridValues": [
                    {
                        "gridCode": "144-下行工作模式",
                        "gridValue": this.formData.downwardWorkingMode
                    }
                ],
                "packageCode": "144-jd-下行-控制接口数据下发"
            }
            // let flag = true
            // data.gridValues.forEach(e => {
            //     if (e.gridValue == undefined || e.gridValue == '' || e.gridValue == null) {
            //         var index = e.gridCode.lastIndexOf("\-");
            //         let str = e.gridCode.substring(index + 1, e.gridCode.length);
            //         this.$message.error(`请输入${str}`)
            //         flag = false
            //     }
            // })
            // if (flag) this.sendAction(data)
            this.sendAction(data)
        },
        configHoppingPattern() {
            let data = {
                "channelCode": "基带",
                "gridValues": [
                    {
                        "gridCode": "148-下行跳频图案",
                        "gridValue": this.formData.DownwardFrequencyHoppingPattern
                    }
                ],
                "packageCode": "148-jd-跳频-下行跳频图案下发"
            }
            this.sendAction(data)
            let temp = {
                "channelCode": "基带",
                "gridValues": [
                    {
                        "gridCode": "150-上行跳频图案",
                        "gridValue": this.formData.UpwardFrequencyHoppingPattern
                    }
                ],
                "packageCode": "150-jd-跳频-上行跳频图案下发"
            }
            // let flag = true
            // temp.gridValues.forEach(e => {
            //     if (e.gridValue == undefined || e.gridValue == '' || e.gridValue == null) {
            //         var index = e.gridCode.lastIndexOf("\-");
            //         let str = e.gridCode.substring(index + 1, e.gridCode.length);
            //         this.$message.error(`请输入${str}`)
            //         flag = false
            //     }
            // })
            // if (flag) this.sendAction(temp)
            this.sendAction(temp)

        },
        handleSend() {

            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    this.configUpstream() //上行
                    this.configDown()
                    this.configFrequencyHopping()//跳频 
                    setTimeout(() => { this.configHoppingPattern() }, 1000) //图案

                } else {
                    console.log('error submit!!');
                    return false;
                }
            });

        },

    }

}
</script>

<style scoped>
.ant-card-bordered {
    height: 100%;
    overflow-y: auto;
}
</style>